layui.use(['element', 'form', 'layer'], function () {
    var form = layui.form,
        // pinyin = layui.pinyin,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery
        ,element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块;

    initTown();

    form.on('select(townIdSelect)', function (data){
        initArea(data)
    });


    /**
     * 所属乡镇下拉框展示
     */
    function initTown(){
        $.ajax({
            type:"post",
            url:ctx+"/town/list",
            data:{"searchParams":"","page":"1","limit":"99"},
            dataType: "json",
            success:function (responseData){
                if (responseData.code === 200 || responseData.code === 0) {
                    var length = responseData.data.length;
                    console.log("乡镇信息数量:"+length);
                    $("#townId").empty();
                    $("#townId").append('<option value=""></option>');
                    for(var i = 0; i < length; i++) {
                        //添加option元素
                        // $("#townId").append("<option value='" + responseData.data[i].id + "'>" + responseData.data[i].name + "</option>");
                        $("#townId").append("<option value='" + responseData.data[i].id + "'> " + responseData.data[i].name + ' - ' + responseData.data[i].fpyName + "</option>");
                    }
                    // 重新渲染
                    form.render('select');
                } else {
                    layer.msg("加载列表失败");
                }
            }
        });
    };


    /**
     * 所属村下拉框展示
     */
    function initArea(data){
        console.log("select------townId--- data.value="+data.value)
        var townIdSelect = data.value;
        $("#areaId").empty()
        // initArea();
        $.ajax({
            type:"post",
            url:ctx+"/area/list",
            data:{"townId":townIdSelect},
            dataType: 'json',
            success:function (responseData){
                if (responseData.code === 200 || responseData.code === 0) {
                    var length = responseData.data.length;
                    console.log("村信息数量:"+length);
                    $("#areaId").empty();
                    $("#areaId").append('<option value=""></option>');
                    for(var i = 0; i < length; i++) {
                        //添加option元素
                        // $("#townId").append("<option value='" + responseData.data[i].id + "'>" + responseData.data[i].name + "</option>");
                        $("#areaId").append("<option value='" + responseData.data[i].id + "'> " + responseData.data[i].name + ' - ' + responseData.data[i].fpyName + "</option>");
                    }
                    // 重新渲染
                    form.render('select');
                } else {
                    layer.msg("加载列表失败");
                }
            }
        });
    };

    form.on("submit(addOrUpdateCustomer)", function (data) {
        var index = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
        //弹出loading
        var url=ctx + "/customer/save";
        if($("input[name='id']").val()){
            url=ctx + "/customer/update";
        }
        $.post(url, data.field, function (res) {
            if (res.code == 200) {
                setTimeout(function () {
                    top.layer.close(index);
                    top.layer.msg("操作成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                }, 500);
            } else {
                layer.msg(
                    res.message, {
                        icon: 5
                    }
                );
            }
        });
        return false;
    });

    $("#closeDlg").click(function (){
        // iframe 页面关闭 添加parent
        parent.layer.closeAll();
    })
});
